package com.fei.socket;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {

	public static void main(String[] args) {
		Socket socket = null;
		ServerSocket server = null;
		try {

			try {
				server = new ServerSocket(9527, 1, InetAddress.getByName("127.0.0.1"));
				System.out.println("服务器启动成功");
			} catch (Exception e) {
				System.out.println("没有启动监听：" + e);
			}

			while (true) {
				try {
					socket = server.accept();
					new HandleMessage(socket);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		} catch (Exception e) {// 出错，打印出错信息
			System.out.println("Error." + e);
		} finally {
			if (socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				} // 关闭Socket
			}
			if (server != null) {
				try {
					server.close();
				} catch (IOException e) {
					e.printStackTrace();
				} // 关闭ServerSocket
			}
		}
	}

}
